package com.administration.controller;

import com.administration.constants.Result;
import com.administration.dao.SysDeptMapper;
import com.administration.pojo.SysDept;
import com.administration.utils.ResultData;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import javax.annotation.Resource;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

@Controller
@RequestMapping("/sysDept")
public class SysDeptController {

    @Resource
    private SysDeptMapper sysDeptMapper;

    @RequestMapping("/dept.do")
    public String deptIndex(){
        return "/static/dept/layui_dept_index.html";
    }

    //查询所有部门信息/查询单个
    @ResponseBody
    @RequestMapping("/selectAllDept.do")
    public ResultData selectAllDept(SysDept sysDept) {
        Map<String,Object> map = new HashMap<>();
        map.put("dept_id",sysDept.getDeptId());
        map.put("deptName",sysDept.getDeptName());
        map.put("status",sysDept.getStatus());

        List<SysDept> list = sysDeptMapper.selectAllSysDept(map);
        for (SysDept sd:list){
            //修改状态显示
            if ("0".equals(sd.getStatus())){
                sd.setStatus("正常");
            }else {
                sd.setStatus("停用");
            }
        }

        return new ResultData<>(Result.SUCCESS.getCode(), "",list.size(), list);
    }

    //通过deptId查询单个部门信息
    @ResponseBody
    @RequestMapping("/selectDeptById.do")
    public ResultData selectDeptById(Integer deptId) {
        SysDept sysDept = sysDeptMapper.selectDeptById(deptId);
        List<SysDept> list = new ArrayList<>();
        list.add(sysDept);
        return new ResultData<>(Result.SUCCESS.getCode(), "",list.size(), list);
    }

    //新增部门信息
    @ResponseBody
    @RequestMapping("/addDept.do")
    public int addDept(SysDept sysDept){

        //关联添加祖级字段信息
        if(!"0".equals(sysDept.getParentId())){
            SysDept dept = sysDeptMapper.selectDeptById(sysDept.getParentId());
            StringBuffer str = new StringBuffer();
            str.append(dept.getAncestors()+",");
            str.append(sysDept.getParentId());
            sysDept.setAncestors(str.toString());
        }
        int addRes = sysDeptMapper.addDept(sysDept);
        return addRes;
    }

    //修改部门信息
    @ResponseBody
    @RequestMapping("/updateDept.do")
    public int updateDept(SysDept sysDept){
        if ("1".equals(sysDept.getStatus())){
            Map<String,Object> map = new HashMap<>();
            map.put("parentId",sysDept.getDeptId());
            List<SysDept> list = sysDeptMapper.selectAllSysDept(map);
            if (list.size()>0){
                //对以该部门为上级部门的部门状态进行判断
                for (SysDept sd:list){
                    if ("0".equals(sd.getStatus())){
                        return -1;
                    }
                }
            }
        }
        int updateRes = sysDeptMapper.updateDept(sysDept);
        return updateRes;
    }

    //删除部门信息
    @ResponseBody
    @RequestMapping("/deleteDept.do")
    public int deleteDept(SysDept sysDept){
        int deleteRes = sysDeptMapper.deleteDeptById(sysDept.getDeptId());
        return deleteRes;
    }
}
